﻿using System;
using System.Collections;
using System.Text;

public class AlipayService
{
    private string _input_charset = "";
    private string _key = "";
    private string _sign_type = "";
    private string gateway = "";
    private string mysign = "";
    private ArrayList sPara = new ArrayList();

    public AlipayService(string partner, string seller_email, string return_url, string notify_url, string show_url, string out_trade_no, string subject, string body, string total_fee, string paymethod, string defaultbank, string encrypt_key, string exter_invoke_ip, string extra_common_param, string buyer_email, string royalty_type, string royalty_parameters, string it_b_pay, string key, string input_charset, string sign_type)
    {
        this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
        this._key = key.Trim();
        this._input_charset = input_charset.ToLower();
        this._sign_type = sign_type.ToUpper();
        this.sPara.Add("service=create_direct_pay_by_user");
        this.sPara.Add("payment_type=1");
        this.sPara.Add("partner=" + partner);
        this.sPara.Add("seller_email=" + seller_email);
        this.sPara.Add("return_url=" + return_url);
        this.sPara.Add("notify_url=" + notify_url);
        this.sPara.Add("_input_charset=" + this._input_charset);
        this.sPara.Add("show_url=" + show_url);
        this.sPara.Add("out_trade_no=" + out_trade_no);
        this.sPara.Add("subject=" + subject);
        this.sPara.Add("body=" + body);
        this.sPara.Add("total_fee=" + total_fee);
        this.sPara.Add("paymethod=" + paymethod);
        this.sPara.Add("defaultbank=" + defaultbank);
        this.sPara.Add("anti_phishing_key=" + encrypt_key);
        this.sPara.Add("exter_invoke_ip=" + exter_invoke_ip);
        this.sPara.Add("extra_common_param=" + extra_common_param);
        this.sPara.Add("buyer_email=" + buyer_email);
        this.sPara.Add("royalty_type=" + royalty_type);
        this.sPara.Add("royalty_parameters=" + royalty_parameters);
        this.sPara.Add("it_b_pay=" + it_b_pay);
        this.sPara = AlipayFunction.Para_filter(this.sPara);
        this.sPara.Sort();
        this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
    }

    public string Build_postform()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + this.gateway + "_input_charset=" + this._input_charset + "\" method=\"post\">");
        int count = this.sPara.Count;
        for (int i = 0; i < count; i++)
        {
            int index = this.sPara[i].ToString().IndexOf('=');
            int length = this.sPara[i].ToString().Length;
            string str = this.sPara[i].ToString().Substring(0, index);
            string str2 = "";
            if ((index + 1) < length)
            {
                str2 = this.sPara[i].ToString().Substring(index + 1);
            }
            builder.Append("<input type=\"hidden\" name=\"" + str + "\" value=\"" + str2 + "\"/>");
        }
        builder.Append("<input type=\"hidden\" name=\"sign\" value=\"" + this.mysign + "\"/>");
        builder.Append("<input type=\"hidden\" name=\"sign_type\" value=\"" + this._sign_type + "\"/></form>");
        builder.Append("<input type=\"button\" name=\"v_action\" value=\"支付宝确认付款\" onClick=\"document.forms['alipaysubmit'].submit();\">");
        return builder.ToString();
    }

    public string Create_url()
    {
        string gateway = this.gateway;
        string str2 = AlipayFunction.Create_linkstring_utf8(this.sPara);
        return (gateway + str2 + "sign=" + this.mysign + "&sign_type=" + this._sign_type);
    }
}

